package com.theexceptions.todo.model;

import java.util.Calendar;

import android.util.Log;

/**
 * TODO currently in the database, there can be multiple reminders for a single
 * task; need to redesign or change constructor
 * 
 * TODO add JavaDoc of what can be null
 * 
 * Information Holder that stores and allows access to the information of a Task
 * object
 * 
 * @author Spencer Powell
 */
public class Task {

	private long taskID;
	private long userID;
	private String name;
	private Calendar time;
	private Location location;
	private Repeat repeat;
	private Reminder reminder;
	private String category;
	private String description;
	private boolean completed;

	/**
	 * 
	 * @param taskID The id of the task. Required. May not be null
	 * @param userID The id of the task's owner. Required. May not be null
	 * @param name The name of the task. Required. May not be null.
	 * @param time
	 * @param location
	 * @param repeat
	 * @param reminder
	 * @param category
	 * @param description
	 * @param completed
	 */
	public Task(long taskID, long userID, String name, Calendar time, Location location, Repeat repeat,
			Reminder reminder, String category, String description, boolean completed) {
		this.taskID = taskID;
		this.name = name;
		this.time = time;
		this.location = location;
		this.repeat = repeat;
		this.reminder = reminder;
		this.category = category;
		this.description = description;
		this.userID = userID;
		this.completed = completed;
	}

	public long getTaskID() {
		return taskID;
	}

	public String getTaskName() {
		return name;
	}

	public Calendar getTime() {
		return time;
	}

	public long getUserID() {
		return userID;
	}

	public Location getLocation() {
		return location;
	}

	public Repeat getRepeat() {
		return repeat;
	}

	public Reminder getReminder() {
		return reminder;
	}

	public String getCategory() {
		return category;
	}

	public String getDescription() {
		return description;
	}
	
	public boolean isCompleted() {
		return completed;
	}

	public void setTaskID(long taskID) {
		this.taskID = taskID;
	}

	public void setTaskName(String name) {
		this.name = name;
	}

	public void setTime(Calendar time) {
		this.time = time;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public void setRepeat(Repeat repeat) {
		this.repeat = repeat;
	}

	public void setReminder(Reminder reminder) {
		this.reminder = reminder;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public boolean toggleCompleted() {
		this.completed = !completed;
		return completed;
	}
	
	@Override
	public boolean equals(Object other) {
		if (!(other instanceof Task)) {
			return false;
		} else {
			Task o = (Task) other;
			
			//TODO make more meaningful comparisons
			return (
					taskID == o.taskID &&
					userID == o.userID &&
					name.equals(o.name) &&
					//(time == null && o.time == null || time.equals(o.time)) &&
					//(location == null && o.location == null || location.equals(o.location)) &&
					//(repeat == null && o.repeat == null || repeat.equals(o.repeat)) &&
					//(reminder == null && o.reminder == null || reminder.equals(o.reminder)) &&
					(category == null && o.category == null || category != null && category.equals(o.category)) &&
					(description == null && o.description == null || description != null && description.equals(o.description)) &&
					completed == o.completed
				);
		}
	}
	
	@Override
	public String toString() {
		int m = time.get(Calendar.MONTH);
		int d = time.get(Calendar.DAY_OF_MONTH);
		int y = time.get(Calendar.YEAR);
		
		String result = String.format("%s (%d/%d/%d)", name, m, d, y);
		// if (isCompleted()) result += " [COMPLETE]";
		
		return result;
	}
}